package com.sky.controller.user;

import com.sky.constant.RedisConstant;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ClassName: UserDishController
 * Package: com.sky.controller.user
 * Description:
 *
 * @Autehor 屈子岩
 * @Create 2024/9/12 16:27
 * @Version 1.0
 */
@RestController
@RequestMapping("user/dish")
@Slf4j
public class UserDishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("list")
    public Result<List<DishVO>> list(Long categoryId){
        log.info("根据分类Id查询菜品");
        String key = RedisConstant.DISH_KEY + categoryId;
        List<DishVO> dish= (List<DishVO>) redisTemplate.opsForValue().get(key);

        if (dish != null && dish.size() > 0){
            return Result.success(dish);
        }
        dish =  dishService.listByCategoryId(categoryId);
        redisTemplate.opsForValue().set(key,dish);
        return Result.success(dish);
    }
}
